package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐信息")
    public Result<SetmealVO> getSetmealAndSetmealDishById(@PathVariable Long id) {
        log.info("获取套餐信息");
        SetmealVO vo= setmealService.getByIdWithDish(id);
        return Result.success(vo);
    }

    @PostMapping
    @ApiOperation("新增套餐信息")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐信息:{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return Result.success("新增套餐信息成功");
    }

    @GetMapping("/page")
    @ApiOperation("套餐信息分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐信息分页查询");
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delet(@RequestParam List<Long> ids) {
        log.info("批量删除菜品信息");
        setmealService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    @PutMapping
    @ApiOperation("修改套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐信息");
        setmealService.updateWithDish(setmealDTO);
        return Result.success("修改套餐信息成功");
    }

    @PostMapping("/status/{status}")
    @ApiOperation("起售或停售套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("修改销售状态");
        setmealService.startOrStop(status,id);
        return Result.success();
    }
}
